package org.hxyjs.dao.impl;

import org.hxyjs.dao.NewsDao;
import org.hxyjs.entity.News;
import org.hxyjs.mapper.NewsMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("newsDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class NewsDaoImpl implements NewsDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private NewsMapper newsMapper;

    public List<News> getAllNewss() throws Exception {
        return newsMapper.selectByExample(null);
    }

    @Override
    public News getNewsByNeno(String ano) throws Exception {
        return newsMapper.selectByPrimaryKey(ano);
    }


    @Override
    public Integer deleteNewsByNeno(String ano) {
        return newsMapper.deleteByPrimaryKey(ano);
    }

    @Override
    public Integer updateNewsByNeno(News news) {
        return newsMapper.updateByPrimaryKey(news);
    }

    @Override
    public Integer addNewsByNeno(News news) {

        news.setNeno(initId());
        return newsMapper.insertSelective(news);
    }

    public String initId() {
        String maxNeno = newsMapper.getmaxNeno();
        return GenNumTools.initId("10", 3, maxNeno);
    }

    @Override
    public List<News> getNewsByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) newsMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return newsMapper.getNewsByPage(map);
    }

}